<?php
/**
 *
 *
 */
namespace app\api\model;

use Think\Loader;
use think\Model;
use think\Db;
use think\File;
use think\Request;
use org\Http;
use org\Upload;
use Image\Image;
use think\Log;
use think\Cache;
class Up extends Model
{

    public function up($type, $filename = 'file', $is_water = false)
    {
        if (cms('qiniu') == 1) {
            $file = request()->file($filename);
            $md5 = $file->hash('md5');
            $n = Db::name('file')->where('md5', $md5)->find();
            if (empty($n)) {
                $driverConfig = array('secrectKey' => cms('secrectKey'), 'accessKey' => cms('accessKey'), 'domain' => cms('domain'), 'bucket' => cms('bucket'));
                $setting['exts'] = explode(',', cms('ext'));
                $setting['maxSize'] = 50 * 1024 * 1024;
                $File = $_FILES['file'];
                $Upload = new Upload($setting, 'Qiniu', $driverConfig);
                $info = $Upload->uploadOne($File);
                if ($info) {
                    $data['sha1'] = $info['sha1'];
                    $data['md5'] = $info['md5'];
                    $data['create_time'] = time();
                    if (empty(session('id'))) {
                        $data['uid'] = 1;
                    } else {
                        $data['uid'] = session('id');
                    }
                    $data['download'] = 0;
                    $data['size'] = $info['size'];
                    $data['name'] = $info['name'];
                    $data['ext'] = $info['ext'];
                    $data['savepath'] = $info['url'];
                    $data['savename'] = $info['savename'];
                    $data['mime'] = $info['type'];
                    $map['md5'] = $info['md5'];
                    $nS = preg_replace("/\\/(.*)/", "", $data['mime']);
                    if ($nS == "image") {
                        $img_info = getimagesize($data['savepath']);
                        $ccS = preg_replace("/[\",=,width]/", "", $img_info[3]);
                        $cc = preg_replace("/eg/", "x ", $ccS);
                        //保存缩略图
                        if (cms('thumb') == 1) {
                            $infos = $file->validate(['size' => 50 * 1024 * 1024, 'ext' => cms('ext')])->move(ROOT_PATH . DS . 'uploads' . DS . 'temporary');
                            $path = DS . 'uploads' . DS . 'temporary' . DS . $infos->getSaveName();
                            $thumbpath = DS . 'uploads' . DS . 'thumb' . DS . date("Y-m-d H:i", time()) . $info['sha1'] . 'thumb.jpg';
                            $image = Image::open(ROOT_PATH . $path);
                            $image->thumb(cms('thumbw'), cms('thumbh'), 1)->save(ROOT_PATH . $thumbpath);
                            $thumb = '' . http() . '' . $_SERVER['SERVER_NAME'] . '' . $thumbpath . '';
                            deleteun(ROOT_PATH . 'uploads/temporary');
                        } else {
                            $thumb = '';
                        }
                    } else {
                        $cc = '';
                        $thumb = '';
                    }
                    $data['thumb'] = $thumb;
                    $data['cc'] = $cc;
                    Db::name('file')->insert($data);
                    $res = Db::name('file')->getLastInsID();
                    if ($res > 0) {
                        return array('code' => 200, 'msg' => '上传成功', 'hasscore' => 0, 'ext' => $data['ext'], 'cc' => $cc, 'thumb' => $thumb, 'size' => size($data['size']), 'id' => $res, 'headpath' => $data['savepath'], 'path' => $data['savepath'], 'md5' => $data['md5'], 'savename' => $data['savename'], 'filename' => $data['name'], 'info' => $info);
                    } else {
                        return array('code' => 0, 'msg' => '上传失败');
                    }
                } else {
                    return array('code' => 0, 'msg' => $Upload->getError());
                }
            } else {
                $cc = $n['cc'];
                $path = $n['savepath'];
                $realpath = WEB_URL . $path;
                return array('code' => 200, 'msg' => '上传成功', 'hasscore' => 1, 'ext' => $n['ext'], 'cc' => $cc, 'thumb' => $n['thumb'], 'id' => $n['id'], 'size' => size($n['size']), 'path' => $path, 'headpath' => $realpath, 'md5' => $n['md5'], 'savename' => $n['savename'], 'filename' => $n['name'], 'info' => $n);
            }
        } else {
            $file = request()->file($filename);
            $md5 = $file->hash('md5');
            $n = Db::name('file')->where('md5', $md5)->find();
            if (empty($n)) {
                $info = $file->validate(['size' => 50 * 1024 * 1024, 'ext' => cms('ext')])->move(ROOT_PATH . DS . 'uploads');
                if ($info) {
                    $path = DS . 'uploads' . DS . $info->getSaveName();
                    $path = str_replace("\\", "/", $path);
                    $realpath = WEB_URL . $path;
                    $data['sha1'] = $info->sha1();
                    $data['md5'] = $info->md5();
                    $data['create_time'] = time();
                    if (empty(session('id'))) {
                        $data['uid'] = 1;
                    } else {
                        $data['uid'] = session('id');
                    }
                    $data['download'] = 0;
                    $data['size'] = $info->getSize();
                    $fileinfo = $info->getInfo();
                    $data['name'] = $fileinfo['name'];
                    $data['ext'] = $info->getExtension();
                    $data['savepath'] = '' . http() . '' . $_SERVER['SERVER_NAME'] . '' . $path . '';
                    $data['savename'] = $info->getFilename();
                    $data['mime'] = $fileinfo['type'];
                    $nS = preg_replace("/\\/(.*)/", "", $data['mime']);
                    if ($nS == "image") {
                        $img_info = getimagesize('' . http() . '' . $_SERVER['SERVER_NAME'] . '' . $path . '');
                        $ccS = preg_replace("/[\",=,width]/", "", $img_info[3]);
                        $cc = preg_replace("/eg/", "x ", $ccS);
                        //保存缩略图
                        if (cms('thumb') == 1) {
                            $thumbpath = DS . 'uploads' . DS . 'thumb' . DS . $info->sha1() . 'thumb.jpg';
                            $image = Image::open(ROOT_PATH . $path);
                            $image->thumb(cms('thumbw'), cms('thumbh'), 1)->save(ROOT_PATH . $thumbpath);
                            $thumb = '' . http() . '' . $_SERVER['SERVER_NAME'] . '' . $thumbpath . '';
                        } else {
                            $thumb = '';
                        }
                    } else {
                        $cc = '';
                        $thumb = '';
                    }
                    $data['thumb'] = $thumb;
                    $data['cc'] = $cc;
                    Db::name('file')->insert($data);
                    $res = Db::name('file')->getLastInsID();
                    if ($res > 0) {
                        return array('code' => 200, 'msg' => '上传D成功', 'hasscore' => 0, 'ext' => $data['ext'], 'cc' => $cc, 'thumb' => $thumb, 'size' => size($data['size']), 'id' => $res, 'path' => '' . http() . '' . $_SERVER['SERVER_NAME'] . '' . $path . '', 'headpath' => $realpath, 'md5' => $data['md5'], 'savename' => $data['savename'], 'filename' => $data['name'], 'info' => $info->getInfo());
                    } else {
                        return array('code' => 0, 'msg' => '上传失败');
                    }
                } else {
                    return array('code' => 0, 'msg' => $file->getError());
                }
            } else {
                $cc = $n['cc'];
                $path = $n['savepath'];
                $realpath = WEB_URL . $path;
                return array('code' => 200, 'msg' => '上传成功', 'hasscore' => 1, 'ext' => $n['ext'], 'cc' => $cc, 'thumb' => $n['thumb'], 'id' => $n['id'], 'size' => size($n['size']), 'path' => $path, 'headpath' => $realpath, 'md5' => $n['md5'], 'savename' => $n['savename'], 'filename' => $n['name'], 'info' => $n);
            }
        }
    }
}